home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / tex / dvivga9.arc / CHARGF.H < prev    next >
Text File  |  1988-05-30  |  11KB  |  345 lines

  1. /* -*-C-*- chargf.h */
  2. /*-->chargf*/
  3. /**********************************************************************/
  4. /******************************* chargf *******************************/
  5. /**********************************************************************/
  6.  
  7. int
  8. chargf(c,outfcn)    /* return 0 on success, and EOF on failure */
  9. BYTE c;            /* current character value */
  10. void (*outfcn)();    /* (possibly NULL) function to output current row */
  11. {
  12.     UNSIGN16 d;            /* step in m index */
  13.     BOOLEAN do_output;        /* FALSE if outfcn is NULL */
  14.     register UNSIGN16 k;    /* loop step */
  15.     register INT16 m,n;        /* column,row indices in image[n][m] */
  16.     long p;            /* pointer into font file */
  17.     BYTE paint_switch;        /* alternates between BLACK and WHITE */
  18.     struct char_entry *tcharptr;/* temporary char_entry pointer */
  19.     register BYTE the_byte;    /* current command byte */
  20.     UNSIGN32 the_word;        /* temporary result holder */
  21.  
  22. /* NB: We only test for equality with BLACK.   WHITE is any non-zero
  23. bit pattern, so we cannot test for equality with it */
  24. #define BLACK ((BYTE)0)
  25. #define WHITE ((BYTE)(!BLACK))
  26.  
  27.     /*******************************************************************
  28.     This function is called to process a single character description in
  29.     the GF font file,  and to set the  character metrics hp, wp,  xoffp,
  30.     and yoffp for it.  The character  description may start either at  a
  31.     special (XXX) command, or at a beginning-of-character (BOC or  BOC1)
  32.     command.   It  processes  the  character  description  up  to,   and
  33.     including, the end-of-character (EOC) command which terminates it.
  34.  
  35.     The GF  raster description  is encoded  in a  complex form,  but  is
  36.     guaranteed to step across raster rows  from left to right, and  down
  37.     from the  top  row  to  bottom  row,  such  that  in  references  to
  38.     image[n][m], m never decreases in a row, and n never increases in  a
  39.     character.  This means that we  only require enough memory space  to
  40.     hold one row, provided that outfcn(c,yoff) is called each time a row
  41.     is     completed.    This   is   an    important   economization    for
  42.     high-resolution output  devices --  e.g. a  10pt character  at  2400
  43.     dots/inch would require about  8Kb for the  entire image, but  fewer
  44.     than 25 bytes for a  single row.  A 72pt  character (such as in  the
  45.     aminch font) would need  about 430Kb for the  image, but only  about
  46.     200 bytes for a row.
  47.  
  48.     Access to bit m in the current row is controlled through the  macros
  49.     SETBIT(m) and  TESTBIT(m) so  we  need not  be concerned  about  the
  50.     details of bit masking.  Too bad C does not have a bit data type!
  51.  
  52.     The row image is recorded in such a way that bits min_m .. max_m are
  53.     mapped onto  bits  0  ..  (max_m -  min_m)  in  img_row[],  so  that
  54.     outfcn(c,yoff) should be relieved of any shifting operations.
  55.  
  56.     outfcn(c,yoff) is NEVER called if either of hp or wp is <= 0, or if
  57.     if it is NULL.
  58.     *******************************************************************/
  59.  
  60.     if ((c < FIRSTPXLCHAR) || (LASTPXLCHAR < c))
  61.     {
  62.     (void)warning(
  63.         "chargf():  Character value out of range for GF font file");
  64.     return(EOF);
  65.     }
  66.     tcharptr = &(fontptr->ch[c]);
  67.  
  68.     p = (long)tcharptr->fontrp;        /* font file raster pointer */
  69.     if (p < 0L)
  70.     {
  71.     (void)warning(
  72.         "chargf():  Requested character not found in GF font file");
  73.     return(EOF);
  74.     }
  75.     if (FSEEK(fontfp,p,0))
  76.     {
  77.     (void)warning(
  78.         "chargf():  FSEEK() failure for GF font file character raster");
  79.     return(EOF);
  80.     }
  81.  
  82.     do_output = (BOOLEAN)(outfcn != (void(*)())NULL);
  83.  
  84.     (void)skgfspec();            /* skip any GF special commands */
  85.  
  86.     the_byte = (BYTE)nosignex(fontfp,(BYTE)1);
  87.     if ((the_byte != GFBOC) && (the_byte != GFBOC1))
  88.     {
  89.     (void)warning(
  90.         "chargf():  GF font file not positioned at BOC or BOC1 command");
  91.     return(EOF);
  92.     }
  93.  
  94.     for (;;)    /* loop with exit at EOC, or at BOC or BOC1 if no rasters */
  95.     {
  96.     switch (the_byte)
  97.     {
  98.     case GFPAINT0:
  99.         paint_switch = (BYTE)(!paint_switch);
  100.         break;
  101.  
  102.     case GFPAINT1:
  103.         d = (UNSIGN16)nosignex(fontfp,(BYTE)1);
  104.         if (do_output && (paint_switch == BLACK))
  105.         {
  106.             for (k = 0; k < d; (++m, ++k))
  107.             SETBIT(m);
  108.         }
  109.         else
  110.         m += (INT16)d;
  111.         paint_switch = (BYTE)(!paint_switch);
  112.         break;
  113.  
  114.     case GFPAINT2:
  115.         d = (UNSIGN16)nosignex(fontfp,(BYTE)2);
  116.         if (do_output && (paint_switch == BLACK))
  117.         {
  118.             for (k = 0; k < d; (++m, ++k))
  119.             SETBIT(m);
  120.         }
  121.         else
  122.         m += (INT16)d;
  123.         paint_switch = (BYTE)(!paint_switch);
  124.         break;
  125.  
  126.     case GFPAINT3:            /* METAFONT never needs this */
  127.         d = (UNSIGN16)nosignex(fontfp,(BYTE)3);    /* NOTE: truncation */
  128.         if (do_output && (paint_switch == BLACK))
  129.         {
  130.             for (k = 0; k < d; (++m, ++k))
  131.             SETBIT(m);
  132.         }
  133.         else
  134.         m += (INT16)d;
  135.         paint_switch = (BYTE)(!paint_switch);
  136.         break;
  137.  
  138.     case GFBOC:    /* beginning-of-character -- long form */
  139.         the_word = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  140.         p = (long)signex(fontfp,(BYTE)4);
  141.         if ((UNSIGN32)c != the_word)
  142.         {
  143.         if (p < 0L)
  144.         {
  145.             (void)warning(
  146.     "chargf():    Requested character not found in back chain in GF font file");
  147.             return(EOF);
  148.         }
  149.         else
  150.              (void)FSEEK(fontfp,p,0);    /* must follow back chain */
  151.         break;
  152.         }
  153.         else
  154.         {
  155.         min_m = (INT16)signex(fontfp,(BYTE)4);
  156.         max_m = (INT16)signex(fontfp,(BYTE)4);
  157.         min_n = (INT16)signex(fontfp,(BYTE)4);
  158.         max_n = (INT16)signex(fontfp,(BYTE)4);
  159.         }
  160.         if ((min_m < MIN_M) || (MAX_M < max_m) ||
  161.         (min_n < MIN_N) || (MAX_N < max_n))
  162.         {
  163.         (void)warning(
  164.             "chargf():  GF font file character box too large for me");
  165.         return(EOF);
  166.         }
  167.         m = min_m;
  168.         n = max_n;
  169.         paint_switch = WHITE;
  170.         img_words = (UNSIGN16)((max_m - min_m + 1 + 31) >> 5);
  171.         if (do_output)
  172.         (void)clrrow();
  173.         tcharptr->hp = (COORDINATE)(max_n - min_n + 1);
  174.         tcharptr->wp = (COORDINATE)(max_m - min_m + 1);
  175.         tcharptr->xoffp = -(COORDINATE)(min_m);
  176.         tcharptr->yoffp = (COORDINATE)(max_n);
  177.         if (!VISIBLE(tcharptr))
  178.         return(0); /* empty character raster -- nothing to output */
  179.         break;
  180.  
  181.     case GFBOC1:        /* beginning-of-character -- short form */
  182.         if (c != (BYTE)nosignex(fontfp,(BYTE)1))
  183.         {
  184.         (void)warning(
  185.             "chargf():    Requested character not found in GF font file");
  186.         return(EOF);
  187.         }
  188.         else
  189.         {
  190.         min_m = (INT16)nosignex(fontfp,(BYTE)1);
  191.         max_m = (INT16)nosignex(fontfp,(BYTE)1);
  192.         min_m = max_m - min_m;
  193.         min_n = (INT16)nosignex(fontfp,(BYTE)1);
  194.         max_n = (INT16)nosignex(fontfp,(BYTE)1);
  195.         min_n = max_n - min_n;
  196.         }
  197.         if ((min_m < MIN_M) || (MAX_M < max_m) ||
  198.         (min_n < MIN_N) || (MAX_N < max_n))
  199.         {
  200.         (void)warning(
  201.             "chargf():  GF font file character box too large for me");
  202.         return(EOF);
  203.         }
  204.         m = min_m;
  205.         n = max_n;
  206.         paint_switch = WHITE;
  207.         img_words = (UNSIGN16)((max_m - min_m + 1 + 31) >> 5);
  208.         if (do_output)
  209.         (void)clrrow();
  210.         tcharptr->hp = (COORDINATE)(max_n - min_n + 1);
  211.         tcharptr->wp = (COORDINATE)(max_m - min_m + 1);
  212.         tcharptr->xoffp = -(COORDINATE)(min_m);
  213.         tcharptr->yoffp = (COORDINATE)(max_n);
  214.         if (!VISIBLE(tcharptr))
  215.         return(0);/* empty character raster -- nothing to output */
  216.         break;
  217.  
  218.     case GFEOC:            /* end-of-character */
  219.         if (do_output)
  220.         {
  221.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  222.         (void)clrrow();        /* clear next one */
  223.         --n;            /* advance to next row below */
  224.         for ( ; n >= min_n ; --n)    /* output any remaining rows */
  225.             (void)(*outfcn)(c,max_n-n);
  226.         }
  227.         return(0);            /* exit outer loop */
  228.  
  229.     case GFSKIP0:
  230.         if (do_output)
  231.         {
  232.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  233.         (void)clrrow();        /* clear next one */
  234.         }
  235.         --n;            /* advance to next row below */
  236.         m = min_m;
  237.         paint_switch = WHITE;
  238.         break;
  239.  
  240.     case GFSKIP1:
  241.         if (do_output)
  242.         {
  243.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  244.         (void)clrrow();        /* clear next one */
  245.         }
  246.         --n;            /* advance to next row below */
  247.         d = (UNSIGN16)nosignex(fontfp,(BYTE)1);
  248.         if (do_output)
  249.         {
  250.         for (k = 0; k < d; (--n,++k))    /* output d white rows */
  251.             (void)(*outfcn)(c,max_n-n);
  252.         }
  253.         else
  254.         n -= (INT16)d;
  255.         m = min_m;
  256.         paint_switch = WHITE;
  257.         break;
  258.  
  259.     case GFSKIP2:
  260.         if (do_output)
  261.         {
  262.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  263.         (void)clrrow();        /* clear next one */
  264.         }
  265.         --n;            /* advance to next row below */
  266.         d = (UNSIGN16)nosignex(fontfp,(BYTE)2);
  267.         if (do_output)
  268.         {
  269.         for (k = 0; k < d; (--n,++k))    /* output d white rows */
  270.             (void)(*outfcn)(c,max_n-n);
  271.         }
  272.         else
  273.         n -= (INT16)d;
  274.         m = min_m;
  275.         paint_switch = WHITE;
  276.         break;
  277.  
  278.     case GFSKIP3:            /* METAFONT never needs this */
  279.         if (do_output)
  280.         {
  281.         (void)(*outfcn)(c,max_n-n);    /* output current row */
  282.         (void)clrrow();        /* clear next one */
  283.         }
  284.         --n;            /* advance to next row below */
  285.         d = (UNSIGN16)nosignex(fontfp,(BYTE)3);    /* NOTE truncation */
  286.         if (do_output)
  287.         {
  288.         for (k = 0; k < d; (++k, --n))    /* output d white rows */
  289.             (void)(*outfcn)(c,max_n-n);
  290.         }
  291.         else
  292.         n -= (INT16)d;
  293.         m = min_m;
  294.         paint_switch = WHITE;
  295.         break;
  296.  
  297.     case GFXXX1:
  298.     case GFXXX2:
  299.     case GFXXX3:
  300.     case GFXXX4:
  301.     case GFYYY:
  302.     case GFNOOP:
  303.         (void)UNGETC((char)the_byte,fontfp);
  304.         (void)skgfspec();
  305.         break;
  306.  
  307.     default:
  308.         if (((BYTE)GFPAINT0 < the_byte) && (the_byte < GFPAINT1))
  309.         {
  310.         d = (UNSIGN16)(the_byte - GFPAINT0);
  311.         if (do_output && (paint_switch == BLACK))
  312.         {
  313.             for (k = 0; k < d; (++m, ++k))
  314.                 SETBIT(m);
  315.         }
  316.         else
  317.             m += (INT16)d;
  318.         paint_switch = (BYTE)(!paint_switch);
  319.         }
  320.         else if (((BYTE)GFNROW0 <= the_byte) && (the_byte <= GFNROWMAX))
  321.         {
  322.         if (do_output)
  323.         {
  324.             (void)(*outfcn)(c,max_n-n);    /* output current row */
  325.             (void)clrrow();    /* clear next one */
  326.         }
  327.         n--;            /* advance to next row below */
  328.         paint_switch = BLACK;
  329.         m = min_m + (UNSIGN16)(the_byte - GFNROW0);
  330.         }
  331.         else
  332.         {
  333.         (void)sprintf(message,
  334.             "chargf():  Unexpected byte %d (0x%02x) in GF font file \
  335. at position %ld",
  336.             the_byte,the_byte,(long)FTELL(fontfp));
  337.         (void)warning(message);
  338.         return(EOF);
  339.         }
  340.         break;
  341.     }                /* end switch */
  342.     the_byte = (BYTE)nosignex(fontfp,(BYTE)1);
  343.     }                    /* end for() */
  344. }
  345.